<?php

namespace App\Admin\Actions\UserAction;

use App\Models\User;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;

class AlipayBindDismiss extends RowAction
{
    public $name = '支付宝绑定解';
    
    public function handle(Model $model)
    {
	    /**
	     * @var $model User
	     */
    	
    	if (!$model->alipay_user_id) {
    		return $this->response()->error('支付宝账号未绑定');
	    }
    	
    	DB::transaction(function () use ($model) {
		    $model->update([
			    'alipay_user_id' => null,
			    'alipay_nickname' => null,
			    'alipay_avatar' => null,
			    'alipay_province' => null,
			    'alipay_city' => null,
			    'alipay_user_no' => null,
		    ]);
		    $model->withdraws()->update(['alipay_user_id' => null]);
	    });
    	
    	
	    return $this->response()->success('支付宝解绑成功')->refresh();
    }
	
	public function dialog()
	{
		$this->confirm('确定解除当前用户的支付宝绑定?');
	}
}
